#include "LogModule.h"
#include <ut_debug.h>
#include <common.h>
#include <xml.h>

#pragma comment(lib, "logutil")

using namespace utility::system;
using namespace utility::log;
using namespace utility::xml;
using namespace std;

bool CLogModule::Init()
{
#ifdef _DEBUG
    CDebugSinkConsole::Instance()->ShowHeader(true);
#ifdef WIN32
    CDebug::Instance()->SetSink(CDebugSinkWindows::Instance());
#else
    CDebug::Instance()->SetSink(CDebugSinkConsole::Instance());
#endif

#else
    // File logger.
#ifdef WIN32
    CDebugSinkFile::Instance()->SetFileDir(get_exe_path());
#else
    CDebugSinkFile::Instance()->SetFileDir(".");
#endif
    CDebugSinkFile::Instance()->ShowHeader(true);
    CDebugSinkFile::Instance()->SetFileHead("LG_");
    CDebugSinkFile::Instance()->SetFileTail(".log");
    CDebug::Instance()->SetSink(CDebugSinkFile::Instance());
#endif

    CDebug::Instance()->TurnOnDebugType(CDebug::e_DefaultType);
    CDebug::Instance()->OutputFileName(false);
    CDebug::Instance()->OutputLineNumber(false);
    CDebug::Instance()->EnableAllDebugLevel();

    string strFileName = get_exe_path();
    strFileName += "/LogConfig.xml";
    if (!_Load(strFileName.c_str()))
    {
        LOG(CRITICAL) << "Failed on loading log configuration";
        return false;
    }
    return true;
}

bool CLogModule::_Load( const char * szFile )
{
    CMarkupSTL xmlDoc;
    if (!xmlDoc.Load(szFile))
    {
        LOG(CRITICAL) << "Failed on open configuration file: " << szFile << " Error:" << xmlDoc.GetError();
        return false;
    }

    if (!xmlDoc.FindElem("configure"))
    {
        LOG(CRITICAL) << "Failed to find XML node: root";
        return false;
    }
    xmlDoc.IntoElem();

    if (!xmlDoc.FindElem("log"))
    {
        LOG(CRITICAL) << "Failed to find XML node log";
        return false;
    }

    m_strPrintCriticalInfo = xmlDoc.GetAttrib("print_critical_info");
    if (m_strPrintCriticalInfo.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution: log.print_critical_info";
        return false;
    }

    m_strPrintImportantInfo = xmlDoc.GetAttrib("print_important_info");
    if (m_strPrintImportantInfo.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution: log.print_important_info";
        return false;
    }

    m_strPrintCommonInfo = xmlDoc.GetAttrib("print_common_info");
    if (m_strPrintCommonInfo.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution : log.print_common_info";
        return false;
    }

    m_strPrintUserCriticalInfo = xmlDoc.GetAttrib("print_user_critical_info");
    if (m_strPrintUserCriticalInfo.length() == 0)
    {
        LOG(CRITICAL) << "Faile to find XML attribution : log.print_user_critical_info";
        return false;
    }

    m_strPrintUserImportantInfo = xmlDoc.GetAttrib("print_user_important_info");
    if(m_strPrintUserImportantInfo.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution : log.print_user_important_info";
        return false;
    }

    m_strPrintUserCommonInfo = xmlDoc.GetAttrib("print_user_common_info");
    if (m_strPrintUserCommonInfo.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution : log.print_user_common_info";
        return false;
    }

    m_strPrintDebugInfo = xmlDoc.GetAttrib("print_debug_info");
    if (m_strPrintDebugInfo.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution : log.print_debug_info";
        return false;
    }

    m_strPrintSrcFileName = xmlDoc.GetAttrib("print_sourcefile_name");
    if (m_strPrintSrcFileName.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution : log.print_sourcefile_name";
        return false;
    }

    m_strPrintLineNum = xmlDoc.GetAttrib("print_line_numer");
    if (m_strPrintLineNum.length() == 0)
    {
        LOG(CRITICAL) << "Failed to find XML attribution :  log.print_line_numer";
        return false;
    }

    if (m_strPrintCriticalInfo == "true")
        CDebug::Instance()->EnableDebugLevel(CRITICAL);
    else
        CDebug::Instance()->DisableDebugLevel(CRITICAL);

    if (m_strPrintImportantInfo == "true")
        CDebug::Instance()->EnableDebugLevel(IMPORTANT);
    else
        CDebug::Instance()->DisableDebugLevel(IMPORTANT);

    if (m_strPrintCommonInfo == "true")
        CDebug::Instance()->EnableDebugLevel(INFO);
    else
        CDebug::Instance()->DisableDebugLevel(INFO);

    if (m_strPrintUserCriticalInfo == "true")
        CDebug::Instance()->EnableDebugLevel(USERCRITICAL);
    else
        CDebug::Instance()->DisableDebugLevel(USERCRITICAL);

    if (m_strPrintUserImportantInfo == "true")
        CDebug::Instance()->EnableDebugLevel(USERIMPORTANT);
    else
        CDebug::Instance()->DisableDebugLevel(USERIMPORTANT);

    if (m_strPrintUserCommonInfo == "true")
        CDebug::Instance()->EnableDebugLevel(USERINFO);
    else
        CDebug::Instance()->DisableDebugLevel(USERINFO);

    if (m_strPrintDebugInfo == "true")
        CDebug::Instance()->EnableDebugLevel(DEBUGINFO);
    else
        CDebug::Instance()->DisableDebugLevel(DEBUGINFO);

    return true;
}
